<script setup>
// 引入相关依赖
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import { useRouter } from 'vue-router'

const isRegister = ref(false)

const loginData = ref({
  username: '',
  password: '',
})

const registerData = ref({
  username: '',
  password: '',
  rePassword: '',
})

// 密码校验
const passwordValidator = (rule, value, callback) => {
  if (value == '') {
    callback(new Error('请再次输入密码'))
  } else if (registerData.value.password!= value) {
    callback(new Error(`两次输入密码不同`))
  } else {
    callback()
  }
  return
}

const router = useRouter()

const register = async () => {
  registerData.value.username = registerData.value.username.trim()
  registerData.value.password = registerData.value.password.trim()
  registerData.value.rePassword = registerData.value.rePassword.trim()
  // 发送注册请求
  axios.post("http://localhost:8090/register/" + registerData.value.username + "/" + registerData.value.password + "")
      .then((response) => {
    console.log(response)
    if (response.data == "success") {
      ElMessage.success("注册成功")
      // router.push("/profile")
      // _this.route.path('/main')
    }
    else {
      ElMessage.error("注册失败:用户名重复或者其他原因！！")
    }
  })
}

const onSubmit = async () => {
  try {
    // 假设这是你的登录API端点
    axios.get("http://localhost:8090/logintest2/" + loginData.value.username + "/" + loginData.value.password + "")
        .then((response) => {
      console.log(response)
      if (response.data === "success") {
        ElMessage.success("登录成功")
        router.push("/profile")
        // _this.route.path('/main')
      }
      else {
        ElMessage.error("登录失败")
      }
    })
  } catch (error) {
    // 请求出错，显示错误信息
    ElMessage.error("请求错误")
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '长度为3-16位', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '长度为6-16位', trigger: 'blur' },
  ],
  rePassword: [
    {
      required: true,
      validator: passwordValidator,
      message: '两次输入密码不同',
      trigger: 'blur',
    },
  ],
}
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">

      <!-- 注册表单 -->
      <el-form
          :rules="rules"
          :model="registerData"
          ref="form"
          size="large"
          autocomplete="off"
          v-if="isRegister"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>

        <el-form-item prop="username">
          <el-input
              v-model="registerData.username"
              :prefix-icon="User"
              placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="registerData.password"
              :prefix-icon="Lock"
              type="password"
              placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="rePassword">
          <el-input
              v-model="registerData.rePassword"
              :prefix-icon="Lock"
              type="password"
              placeholder="请再次输入密码"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="register" style="width: 100%; height: 40px;">注册</el-button>
        </el-form-item>

        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 返回
          </el-link>
        </el-form-item>

      </el-form>


      <!-- 登录表单 -->
      <el-form
          :rules="rules"
          :model="loginData"
          ref="form"
          size="large"
          autocomplete="off"
          v-else
      >
        <el-form-item>
          <h1 style="transform: translate(110px);font-size: 50px;">登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
              v-model="loginData.username"
              :prefix-icon="User"
              placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="loginData.password"
              :prefix-icon="Lock"
              type="password"
              placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              @click="onSubmit"
              auto-insert-space
              style="width: 100%; height: 40px"
          >登录</el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true">
            注册 →
          </el-link>
        </el-form-item>

      </el-form>
    </el-col>
  </el-row>
</template>

<style>
/* 你可以在这里添加全局样式，或者直接在组件样式中覆盖 */
.el-button {
  height: 40px; /* 假设这是输入框的高度 */
  border-radius: 5px; /* 调整按钮圆角 */
  transition: background-color 0.3s ease, box-shadow 0.3s ease; /* 添加过渡效果 */
}

.el-button:hover {
  background-color: #e6e6e6; /* 鼠标悬停时改变背景颜色 */
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); /* 鼠标悬停时添加阴影 */
}

.login-page {
  height: 100vh;
  background-color: #fff;
  background-image: url('@/assets/kity.jpg'); /* 替换为你实际的图片路径 */
  background-size: cover; /* 让图片铺满整个背景，可根据需求换为 'contain' 等其他值调整展示效果 */
  background-position: center; /* 图片在背景中的位置，这里设置为居中 */
  background-repeat: no-repeat; /* 不重复图片 */
}


.form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  user-select: none;
  border: 1px solid #ccc; /* 添加边框，颜色可以根据需求调整 */
  border-radius: 10px; /* 整体设置圆角 */
  padding: 20px; /* 添加内边距，让内容看起来更协调，可按需调整 */
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
  max-height: 400px;
  margin-top: 200px;
}

.el-input {
  margin-bottom: 15px; /* 增加输入框之间的间距 */
  border-radius: 5px; /* 设置输入框圆角 */
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); /* 添加输入框阴影 */
  transition: box-shadow 0.3s ease; /* 添加阴影过渡效果 */

}

.el-input:focus {
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2); /* 输入框聚焦时加深阴影 */
}

.flex {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
</style>